package com.fingard.dsp.bank.directbank.czb02;

import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;

public class CZB02BatPayRec extends CZB02Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
        if("918801CNVRT".equals(p_req9188.reqHeader.commandCode)){
            if(p_req9188.getTotalNum() == 1){
                ReqSingle req9108 = new ReqSingle(p_req9188);//批量转单笔
                CZB02RealTime tmpRealTime = new CZB02RealTime();
                tmpRealTime.setSbLog(this.sbLog);
                tmpRealTime.setLogFlushTime(this.logFlushTime);
                tmpRealTime.reqHeader = req9108.reqHeader;
                tmpRealTime.bankFront = this.bankFront;
                String idxDir = this.getDataIndexDir(p_req9188.getPayDateTime());
                String filePath = idxDir + File.separator+ p_req9188.reqBatchNO + "_payAct.txt";
                FileHelper.writeFile(filePath, req9108.singleDetail.payAct, "gbk");
                req9108.singleDetail.askNumber = p_req9188.reqBatchNO;//批次号作为单笔流水号

                RetAtsSingle retAts9108 = tmpRealTime.d_9108_RealTimeRec(req9108);
                retAts9108.fillToBatRetAts(retAts);
            }else{
                retAts.fillBatchFailed(p_req9188, FailCode.E9981, "总笔数需为1");
            }
        }else{
            retAts.fillBatchFailed(p_req9188, FailCode.E9981, "未实现该接口");
        }
        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req9198.reqHeader);
        if("918801CNVRT".equals(p_req9198.reqHeader.commandCode)){
            ReqQrySingle req9144 = new ReqQrySingle(p_req9198);//批量转单笔
            CZB02RealTime tmpRealTime = new CZB02RealTime();
            tmpRealTime.setSbLog(this.sbLog);
            tmpRealTime.setLogFlushTime(this.logFlushTime);
            tmpRealTime.reqHeader = req9144.reqHeader;
            tmpRealTime.bankFront = this.bankFront;
            String idxDir = this.getDataIndexDir(p_req9198.getBgnDateTime());
            String filePath = idxDir + File.separator+ p_req9198.reqBatchNO + "_payAct.txt";
            String payAct = FileHelper.readFileText(filePath, "gbk");
            this.WriteBankLogLn("取到的付方账号为："+payAct);
            req9144.payAct = payAct;
            if(StringHelper.isNullOrEmpty(payAct)){
                retAts.respCode = "-1";
                retAts.respInfo = "未取到付方账号，请检查是否存在文件"+filePath;
            }else{
                RetAtsSingle ret9144 = tmpRealTime.d_9144_QryRealTimeRec(req9144);
                ret9144.fillToBatRetAts(retAts);
            }
        }else{
            retAts.respCode = "-1";
            retAts.respInfo = "未实现该接口";
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}
